package com.aaa.behind.controller;


import com.aaa.behind.service.ITbBottomArticleService;
import com.aaa.entity.TbBottomArticle;
import com.aaa.util.PageInfo;
import com.aaa.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author whj
 * @since 2023-02-01
 */
@RestController
@RequestMapping("/tb-bottom-article")
public class TbBottomArticleController {

    @Autowired
    private ITbBottomArticleService tbBottomArticleService;

    @PostMapping("getAllBottomArticle")
    public Result getAllBottomArticle(@ModelAttribute PageInfo pageInfo){
        return new Result(tbBottomArticleService.getAllBottomArticle(pageInfo));
    }


    @PostMapping("getAllfurryBottomArticle")
    public Result getAllfurryBottomArticle(@ModelAttribute PageInfo pageInfo,TbBottomArticle tbBottomArticle){
        return new Result(tbBottomArticleService.getAllfurryBottomArticle(pageInfo,tbBottomArticle));
    }

    @PostMapping("changeStatus/{id}/{isDisabale}")
    public Result changeStatus(@PathVariable String id,@PathVariable String isDisabale){
        TbBottomArticle tbBottomArticle = new TbBottomArticle();
        tbBottomArticle.setId(id);
        tbBottomArticle.setIsDisable(isDisabale);
        tbBottomArticle.setGmtModified(LocalDateTime());

        return new Result(tbBottomArticleService.updateById(tbBottomArticle));
    }

    @PostMapping("editBottomArticle")
    public Result editBottomArticle(TbBottomArticle tbBottomArticle){
        tbBottomArticle.setGmtModified(LocalDateTime());
        return new Result(tbBottomArticleService.saveOrUpdate(tbBottomArticle));
    }

    @PostMapping("addBottomArticle")
    public Result addBottomArticle(TbBottomArticle tbBottomArticle){
        tbBottomArticle.setIsDisable("0");
        tbBottomArticle.setIsDeleted(0);
        tbBottomArticle.setGmtCreate(LocalDateTime());
        tbBottomArticle.setGmtModified(LocalDateTime());
        return new Result(tbBottomArticleService.saveOrUpdate(tbBottomArticle));
    }

    public String LocalDateTime(){
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String format = dateTimeFormatter.format(now);
        return format;
    }
}

